package com.ruoyi.web.controller.exam;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.egdk.api.mapp.ElPaperMapper;
import com.egdk.api.mapp.ElPaperQuAnswerMapper;
import com.egdk.api.mapp.ElPaperQuMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.exam.domain.PExam;
import com.ruoyi.exam.mapper.PExamMapper;
import com.ruoyi.web.controller.exam.dto.*;
import com.ruoyi.web.controller.exam.service.StudentExamRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/exam/api")
public class StudentExamRestController {

    @Autowired
    private PExamMapper pExamMapper;
    @Autowired
    private StudentExamRestService studentExamRestService;

    @PostMapping("/exam/exam/detail")
    public AjaxResult detail(@RequestBody JSONObject object) {
        String examId = object.getString("id");
        PExam exam = pExamMapper.selectPExamById(Long.valueOf(examId));
        JSONObject ret = new JSONObject();
        ret.put("content", exam.getExamName());
        ret.put("createTime", DateUtil.format(exam.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
        ret.put("id", examId);
        ret.put("title", exam.getExamName());
        ret.put("totalTime", exam.getDuration());
        ret.put("totalScore", exam.getTotalScore());
        ret.put("qualifyScore", exam.getPassScore());
        ret.put("openType", exam.getType()+"");
        return AjaxResult.success(ret);
    }


    @PostMapping("/paper/paper/create-paper")
    public AjaxResult createPaper(@RequestBody JSONObject object) {
        String examId = object.getString("examId");
        String paperId = studentExamRestService.createPaper(SecurityUtils.getUserId(), examId);
        JSONObject ret = new JSONObject();
        ret.put("id", paperId);
        return AjaxResult.success(ret);
    }

    @PostMapping("/paper/paper/paper-detail")
    public AjaxResult paperDetail(@RequestBody JSONObject object) {
        String id = object.getString("id");
        //根据ID删除
        ExamDetailRespDTO respDTO = studentExamRestService.paperDetail(id);
        return AjaxResult.success(respDTO);
    }

    @PostMapping("/paper/paper/qu-detail")
    public AjaxResult quDetail(@RequestBody JSONObject object) {
        String paperId = object.getString("paperId");
        String quId = object.getString("quId");
        PaperQuDetailDTO respDTO = studentExamRestService.findQuDetail(paperId, quId);
        return AjaxResult.success(respDTO);
    }

    @PostMapping("/paper/paper/fill-answer")
    public AjaxResult fillAnswer(@RequestBody PaperAnswerDTO reqDTO) {
        studentExamRestService.fillAnswer(reqDTO);
        return AjaxResult.success();
    }


    @PostMapping("/paper/paper/hand-exam")
    public AjaxResult handExam(@RequestBody BaseIdReqDTO reqDTO) {
        studentExamRestService.handExam(reqDTO.getId());
        return AjaxResult.success();
    }

    ///paper/paper/paper-result
    @PostMapping("/paper/paper/paper-result")
    public AjaxResult paperResult(@RequestBody BaseIdReqDTO reqDTO) {
        ExamResultRespDTO respDTO = studentExamRestService.paperResult(reqDTO.getId());
        return AjaxResult.success(respDTO);
    }


    @PostMapping("/user/exam/my-paging")
    public AjaxResult myPaging(@RequestBody PagingReqDTO<UserExamReqDTO> reqDTO) {
        //分页查询并转换
        return studentExamRestService.myPaging(reqDTO);
    }

    ///paper/paper/paging
    @PostMapping("/paper/paper/paging")
    public AjaxResult paging(@RequestBody PagingReqDTO<GetMyExamDTO> reqDTO) {
        //分页查询并转换
        return studentExamRestService.paging(reqDTO);
    }

    //user/wrong-book/paging
    @PostMapping("/user/wrong-book/paging")
    public AjaxResult wrongBookPaging(@RequestBody PagingReqDTO<UserBookDTO> reqDTO) {
        //分页查询并转换
        return studentExamRestService.wrongBookPaging(reqDTO);
    }

    //user/wrong-book/delete
    @RequestMapping(value = "/user/wrong-book/delete", method = { RequestMethod.POST})
    public AjaxResult delete(@RequestBody BaseIdsReqDTO reqDTO) {
        //根据ID删除
        studentExamRestService.removeByIds(reqDTO.getIds());
        return AjaxResult.success();
    }

    @RequestMapping( "/user/wrong-book/next")
    public AjaxResult nextQu(@RequestBody UserBookDTO reqDTO) {
        //根据ID删除
        String quId = studentExamRestService.findNext(reqDTO.getExamId(), reqDTO.getQuId());
        return AjaxResult.success(new BaseIdRespDTO(quId));
    }

    ///qu/qu/detail
    @RequestMapping( "/qu/qu/detail")
    public AjaxResult qudetail(@RequestBody BaseIdReqDTO reqDTO) {
        QuDetailDTO dto = studentExamRestService.qudetail(reqDTO.getId());
        return AjaxResult.success(dto);
    }


}